package com.example.game

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    private lateinit var playerNameText: TextView
    private lateinit var playerGenderText: TextView
    private lateinit var playerStudentIdText: TextView
    private lateinit var playerPhoneText: TextView
    private lateinit var playerEmailText: TextView
    private lateinit var playerHobbyText: TextView
    private lateinit var playerScoreText: TextView
    private lateinit var editInfoButton: Button
    private lateinit var startGameButton: Button

    // Player info data
    private lateinit var playerName: String
    private lateinit var playerGender: String
    private lateinit var playerStudentId: String
    private lateinit var playerPhone: String
    private lateinit var playerEmail: String
    private lateinit var playerHobby: String
    private var playerScore: Int = 0

    // Activity result launcher for EditInfoActivity
    private val editInfoLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data
            data?.let {
                playerName = it.getStringExtra(EXTRA_PLAYER_NAME) ?: playerName
                playerGender = it.getStringExtra(EXTRA_PLAYER_GENDER) ?: playerGender
                playerStudentId = it.getStringExtra(EXTRA_PLAYER_STUDENT_ID) ?: playerStudentId
                playerPhone = it.getStringExtra(EXTRA_PLAYER_PHONE) ?: playerPhone
                playerEmail = it.getStringExtra(EXTRA_PLAYER_EMAIL) ?: playerEmail
                playerHobby = it.getStringExtra(EXTRA_PLAYER_HOBBY) ?: playerHobby
                updatePlayerInfoDisplay()
            }
        }
    }

    // Activity result launcher for CardGameActivity
    private val gameResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data
            data?.let {
                val gameScore = it.getIntExtra(EXTRA_GAME_SCORE, 0)
                if (gameScore > playerScore) {
                    playerScore = gameScore
                    updatePlayerInfoDisplay()
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        // Initialize player data
        initializePlayerData()

        // Initialize views
        initializeViews()

        // Set up button click listeners
        setupButtonListeners()

        // Update display with initial data
        updatePlayerInfoDisplay()

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initializePlayerData() {
        playerName = getString(R.string.default_name)
        playerGender = getString(R.string.default_gender)
        playerStudentId = getString(R.string.default_student_id)
        playerPhone = getString(R.string.default_phone)
        playerEmail = getString(R.string.default_email)
        playerHobby = getString(R.string.default_hobby)
        playerScore = getString(R.string.default_score).toIntOrNull() ?: 0
    }

    private fun initializeViews() {
        playerNameText = findViewById(R.id.player_name_text)
        playerGenderText = findViewById(R.id.player_gender_text)
        playerStudentIdText = findViewById(R.id.player_student_id_text)
        playerPhoneText = findViewById(R.id.player_phone_text)
        playerEmailText = findViewById(R.id.player_email_text)
        playerHobbyText = findViewById(R.id.player_hobby_text)
        playerScoreText = findViewById(R.id.player_score_text)
        editInfoButton = findViewById(R.id.edit_info_button)
        startGameButton = findViewById(R.id.start_game_button)
    }

    private fun setupButtonListeners() {
        editInfoButton.setOnClickListener {
            val intent = Intent(this, EditInfoActivity::class.java).apply {
                putExtra(EXTRA_PLAYER_NAME, playerName)
                putExtra(EXTRA_PLAYER_GENDER, playerGender)
                putExtra(EXTRA_PLAYER_STUDENT_ID, playerStudentId)
                putExtra(EXTRA_PLAYER_PHONE, playerPhone)
                putExtra(EXTRA_PLAYER_EMAIL, playerEmail)
                putExtra(EXTRA_PLAYER_HOBBY, playerHobby)
            }
            editInfoLauncher.launch(intent)
        }

        startGameButton.setOnClickListener {
            val intent = Intent(this, CardGameActivity::class.java)
            gameResultLauncher.launch(intent)
        }
    }

    private fun updatePlayerInfoDisplay() {
        playerNameText.text = getString(R.string.player_name_label) + playerName
        playerGenderText.text = getString(R.string.player_gender_label) + playerGender
        playerStudentIdText.text = getString(R.string.player_student_id_label) + playerStudentId
        playerPhoneText.text = getString(R.string.player_phone_label) + playerPhone
        playerEmailText.text = getString(R.string.player_email_label) + playerEmail
        playerHobbyText.text = getString(R.string.player_hobby_label) + playerHobby
        playerScoreText.text = getString(R.string.player_score_label) + playerScore.toString()
    }

    companion object {
        const val EXTRA_PLAYER_NAME = "extra_player_name"
        const val EXTRA_PLAYER_GENDER = "extra_player_gender"
        const val EXTRA_PLAYER_STUDENT_ID = "extra_player_student_id"
        const val EXTRA_PLAYER_PHONE = "extra_player_phone"
        const val EXTRA_PLAYER_EMAIL = "extra_player_email"
        const val EXTRA_PLAYER_HOBBY = "extra_player_hobby"
        const val EXTRA_GAME_SCORE = "extra_game_score"
    }
}